//
//  LoginViewController.m
//  OTOClient
//
//  Created by Suny on 15/4/9.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "LoginViewController.h"
#import "SignUpViewController.h"
#import "UserService.h"
#import "MMProgressHUD.h"
#import <ShareSDK/ShareSDK.h>
@interface LoginViewController ()
@property (nonatomic, strong)UserService *service;
@end

@implementation LoginViewController
DEF_SINGLETON()

- (UserService *)service
{
	if (!_service)
	{
		_service = [UserService serviceWithDefaultBaseUrlString];
	}
	return _service;
}

- (instancetype)init
{
	if (self = [super init]) 
	{
		self.hasNavigationBar = NO;
		self.signUpViewController = [[SignUpViewController alloc] init];
	}
	return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	
	self.navigationItem.leftBarButtonItem = self.defaultBackButtonItem;
	
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setImage:[UIImage imageNamed:@"navigationbar_back_icon"] forState:UIControlStateNormal];
	[button setTitle:@"返回" forState:UIControlStateNormal];

	[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
	
	button.frame = CGRectMake(20,self.view.height - 100, 100, 44);
	[self.view addSubview:button];
	
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loginToServeByUserId:(NSString *)userId andPassword:(NSString *)password
{
	UserInfo *user = [[UserInfo alloc] init];
	user.loginName = userId;
	user.password  = password;
	
	[self.service loginUser:user
		WithFinishBlock:^(BaseResponse *response) {			
			if (response.success) 
			{
				[[LoginUserInfo sharedInstance] resetAllVarsByContent:response.object];
				

				[self showProgressHUDCompleteMessage:@"登陆成功"];
				[[NSNotificationCenter defaultCenter] postNotificationName:kLoginSucceedNotificationName object:nil];
				
				
			}
			else 
			{
				[self showErrorMessage:response.desc];


			}
		}];

    
}



- (void)GetUserInfo:(UIButton *)sender {
    ShareType type = 0;
    switch (sender.tag) {
        case 1:
            type = ShareTypeSinaWeibo;
            break;
        case 2:
            type = ShareTypeTencentWeibo;
            break;
        case 3:
            type = ShareTypeRenren;
            break;
        case 4:
            type = ShareTypeQQSpace;
            break;
        case 5:
            type = ShareTypeWeixiSession;
            break;
        case 6:
            type = ShareTypeFacebook;
            break;
        case 7:
            type = ShareTypeTwitter;
        default:
            break;
    }
    [ShareSDK getUserInfoWithType:type authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result) {
            NSLog(@"授权登陆成功，已获取用户信息");
            NSString *uid = [userInfo uid];
            NSString *nickname = [userInfo nickname];
            NSString *profileImage = [userInfo profileImage];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:[NSString stringWithFormat:@"授权登陆成功,用户ID:%@,昵称:%@,头像:%@",uid,nickname,profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            NSLog(@"source:%@",[userInfo sourceData]);
            NSLog(@"uid:%@",[userInfo uid]);
            
            
        }else{
            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:@"授权失败，请看日记错误描述" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];
}


@end
